﻿/* Copyright (c) 2011-2012, Zetatron Consulting
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

* Neither the name of Zetatron Consulting nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AionCxxLibrary.Packets;
using AionCxxLibrary.Network;
using AuthenticationServer.Services;
using AionCxxLibrary.Utilities;
using AionCxxLibrary.Enums;
using System.Net;
using System.Configuration;

namespace AuthenticationServer.Packets
{
    public class SPkt_Init : AionServerPacket
    {
        public override void WritePacket()
        {
            Crypted = false;

            WriteInt(Connection.GetHashCode()); // SessionID
            WriteInt(0xc621); // AION Protocol Revision
            WriteBytes(((AuthenticationCrypter)Connection.Crypter).EncryptedRSAModulus); // RSA Public Key

            WriteBytes(new byte[16]); // Unknown

            WriteBytes(((AuthenticationCrypter)Connection.Crypter).player_key); // Blowfish Key
            WriteInt(197635);
            WriteInt(2097152);
        }
    }

    public class SPkt_GameGuard : AionServerPacket
    {
        public override void WritePacket()
        {
            WriteInt(Connection.GetHashCode());
            WriteBytes(new byte[16]);
        }
    }

    public class SPkt_LoginFailed : AionServerPacket
    {
        AuthenticationResponse resp;
        public SPkt_LoginFailed(AuthenticationResponse response)
        {
            resp = response;
        }

        public override void WritePacket()
        {
            WriteInt((int)resp);
        }
    }

    public class SPkt_Login : AionServerPacket
    {
        public override void WritePacket()
        {
            AuthenticationConnection con = (AuthenticationConnection)Connection;
            WriteInt(con.AccountID);
            WriteInt(con.Key_Login);
            WriteLong(0);
            WriteInt(0x3ea);
            WriteBytes(new byte[28]);
        }
    }

    public class SPkt_RealmList : AionServerPacket
    {
        public override void WritePacket()
        {
            WriteByte(1); // Size of list
            WriteByte(1); // Last Server (if -1, player gets the dialog "Would you like to connect to the recommended server XXX?")

            byte MaxRealmID = 0;
            foreach(RealmInformations realm in RealmService.Realms.Realms)
            {
                if (realm.ID > MaxRealmID)
                    MaxRealmID = realm.ID;
                WriteByte(realm.ID);                                                // Server ID
                WriteBytes(Utility.IPAddressToByteArray(realm.Address));            // Server IP
                WriteInt(realm.Port);                                               // Server Port
                WriteByte(0);                                                       // Age Limit
                WriteByte(1);                                                       // Server Type (0 = PvE, 1 = PvP)
                WriteShort(0);                                                      // TODO Current Players
                WriteShort(realm.MaxPlayers);                                       // TODO Max Players
                WriteByte(1);                                                       // Server Status (0 = Down, 1 = Up)
                WriteInt(1);                                                        // Server Bits
                WriteByte(1);                                                       // Server Brackets
            }

            WriteShort((short)(MaxRealmID + 1)); // MaxID + 1
            byte autoConnect = 0;
            if(ConfigurationManager.AppSettings["WorldAutoConnect"].ToString() == "true")
                autoConnect = 1;
            WriteByte(autoConnect); // Autoconnect 0 = Off, 1 = On

            for (int i = 1; i < MaxRealmID; i++)
            {
                WriteByte(0); // Character Count
            }

        }
    }

    public class SPkt_JoinWorld : AionServerPacket
    {
        int serverid;
        public SPkt_JoinWorld(int serverId)
        {
            serverid = serverId;
        }
        public override void WritePacket()
        {
            AuthenticationConnection con = (AuthenticationConnection)Connection;
            WriteInt(con.Key_World1);
            WriteInt(con.Key_World2);
            WriteByte((byte)serverid);
        }
    }

    public class SPkt_JoinWorldFailed : AionServerPacket
    {
        AuthenticationResponse response;
        public SPkt_JoinWorldFailed(AuthenticationResponse resp)
        {
            response=resp;
        }
        public override void WritePacket()
        {
            WriteInt((int)response);
        }
    }

    public class SPkt_Reconnect : AionServerPacket
    {
        public override void WritePacket()
        {
            AuthenticationConnection con = ((AuthenticationConnection)Connection);
            WriteInt(con.AccountID);
            WriteInt(con.Key_Login);
        }
    }

}
